﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    public class UIActivityOnSail : UIActivityTemplate
    {
        /// <summary>
        /// 半价和折扣模板
        /// </summary>
        public UILabel topTips;
        public UILabel activityTime;
        public GameObject itemExchangePref;
        public GameObject itemBuyPref;
        public GameObject singleItem;
        public ActivityType type;

        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public GameObject[] boys;
        public GameObject[] bgs;

        
        public override void OnCoverPageRemove()
        {
            base.OnCoverPageRemove();
        }
        protected override void PrepareData()
        {
            switch (type)
            {
                case ActivityType.HalfDiscountSale:
                    SetBgAndPerson(0);
                    topTips.text = TableTextBlo.GetText("HalfPrice_LimitBuy_tip");
                    break;
                case ActivityType.DiscountSale:
                    SetBgAndPerson(1);
                    topTips.text = TableTextBlo.GetText("Discount_LimitBuy_tip");
                    break;
                case ActivityType.TimeLimitExchange:
                    SetBgAndPerson(2);
                    itemBuyPref = itemExchangePref;
                    topTips.text = TableTextBlo.GetText("Exchange_LimitBuy_tip");
                    break;
                case ActivityType.TimeLimitSail:
                    SetBgAndPerson(3);
                    itemBuyPref = itemExchangePref;
                    topTips.text = TableTextBlo.GetText("Sail_LimitBuy_tip");
                    break;
                default:
                    break;
            }
            activityType = type;
        }

        void SetBgAndPerson(int flag)
        {
            for (int i = 0; i < bgs.Length; i++)
            {
                bgs[i].gameObject.SetActive(false);
                boys[i].gameObject.SetActive(false);
            }
            bgs[flag].gameObject.SetActive(true);
            boys[flag].gameObject.SetActive(true);
        }

        public override void Show()
        {
            base.Show();
            CSActivityShopInfo msg = new CSActivityShopInfo();
            msg.activityId = this.activityId;
            NetAPI.SendHttp(OpDefine.CSActivityShopInfo, msg, MsgCallback);
        }
        void MsgCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCActivityShopInfo msg = packet.kBody as SCActivityShopInfo;

                if (msg.shopItemList.Count <= 0)
                {
                    Debug.Log("活动列表为0");
                    return;
                }
                activityId = msg.activityId;
                //时间
                System.DateTime endTime = ServerTimerTool.Java2CSTimeLocale(msg.endTime);
                System.DateTime startTime = ServerTimerTool.Java2CSTimeLocale(msg.startTime);
                string endTimeStr = endTime.Month.ToString() + TableTextBlo.GetText("month") + endTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", endTime.Hour, endTime.Minute, endTime.Second);
                string startTimeStr = startTime.Month.ToString() + TableTextBlo.GetText("month") + startTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", startTime.Hour, startTime.Minute, startTime.Second);
                activityTime.text = startTimeStr + TableTextBlo.GetText("to") + endTimeStr;

                UpdateItemList(msg.shopItemList);
            }
        }

        public void UpdateItemList(List<ActivityShopItem> list)
        {
            if (list != null)
            {
                list.Sort(delegate (ActivityShopItem a, ActivityShopItem b)
                {
                    if (a.index < b.index)
                        return -1;
                    else if (a.index > b.index)
                        return 1;
                    return 0;
                });
            }

            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();

            for (int i = 0; i < list.Count; i++)
            {
                if (type == ActivityType.TimeLimitExchange || type == ActivityType.TimeLimitSail)
                {
                    GameObject obj = Instantiate(itemExchangePref);
                    obj.SetActive(true);
                    simpleGrid.AddChild(obj);
                    var script = obj.GetComponent<OnSailItem>();
                    script.m_update = UpdateItemList;
                    script.SetItem(list[i], activityId);
                }
                else
                {
                    if(list[i].getResource.Count > 1)
                    {
                        GameObject obj = Instantiate(itemBuyPref);
                        obj.SetActive(true);
                        simpleGrid.AddChild(obj);
                        var script = obj.GetComponent<OnSailItem>();
                        script.m_update = UpdateItemList;
                        script.SetReward(list[i], activityId);
                    }
                    else
                    {
                        GameObject obj = Instantiate(singleItem);
                        obj.SetActive(true);
                        simpleGrid.AddChild(obj);
                        var script = obj.GetComponent<OnSailItem>();
                        script.m_update = UpdateItemList;
                        script.SetSingleReward(list[i], activityId);
                    }
                }
            }
            //scrollView.ResetPosition();
        }
    }
}


